# FLOW : > INVITE, < 180, < 200, > ACK, > BYE, < 200
#

module VoipControllers

require 'base_controller'

class PlayrecController < SIP::BaseController 

  # change the directive below to true to enable transaction usage.
  # If you do that then make sure that your controller is also 
  # transaction aware. i.e does not try send ACK to non-2xx responses,
  # does not send 100 Trying response etc.

  transaction_usage :use_transactions=>false

  # change the directive below to true to start after loading.
  start_on_load false

  def initialize
    print "\n\n SCRIPT: User "+ SipperConfigurator[:ScriptUser].to_s() +" \n\n"
    print "		>> REGISTER\n"
    contact = SipperConfigurator[:LocalSipperIP]+":"
    r = Request.create_initial("REGISTER", "sip:" + SipperConfigurator[:DefaultRIP],
         :from=>"sip:"+ SipperConfigurator[:ScriptUser].to_s() +"@"+ SipperConfigurator[:DefaultRIP], :to=>"sip:"+ SipperConfigurator[:ScriptUser].to_s() +"@"+ SipperConfigurator[:DefaultRIP],
         :contact=>"sip:"+ SipperConfigurator[:ScriptUser].to_s() +"@"+ SipperConfigurator[:LocalSipperIP]+":"+SipperConfigurator[:LocalSipperPort].to_s(),
         :expires=>"300",
         :cseq=>"1 REGISTER",
         :p_session_record=>"msg-info")
    r.contact.q="0.9"
    session = create_udp_session(SipperConfigurator[:DefaultRIP], SipperConfigurator[:DefaultRP])
    session.send(r)
  end

  def on_invite(session)
    print "\n\n[sipper] on_invite \n\n"
  end

  def on_register(session)
    print "\n\n[sipper] on_register \n\n"
  end

  def on_success_res_for_invite(session)
    print "\n\n[sipper] on_success_res_for_invite \n\n"
      print "		<< ACK\n"

      print "\n\n[info] -- Hablando... --\n\n"

      session.make_new_offer

      session.set_media_attributes(:play_spec=>"PLAY_REPEAT rec.au")
=begin
      if SipperConfigurator[:TestType] == "play"
	print "\n[info] -- Reproducción de un mensaje --\n"
	#session.set_media_attributes(:play_spec=>"PLAY_REPEAT hello_sipper.au")
	session.set_media_attributes(:play_spec=>"PLAY_REPEAT hello_sipper.au")
      elsif SipperConfigurator[:TestType] == "record"
	print "\n[info] -- Grabación de un mensaje --\n"
	session.set_media_attributes(:play_spec=>'',:rec_spec=>'rec.au')
      end
=end

      # UN ACK SE ENVIA PARA CONFIRMAR QUE SABEMOS QUE EL INTERLOCUTOR HA DESCOLGADO EL TELEFONO
      session.request_with('ACK')

=begin
      if SipperConfigurator[:ScriptHolds] == true
	print "\n\n[info] -- El SCRIPT colgará tras "+ SipperConfigurator[:CallTime].to_s() +" segundos --\n\n"
	sleep SipperConfigurator[:CallTime]
	print "		>> BYE\n"
	session.request_with('BYE') #if session[:for_carol]
      elsif
	print "\n\n[info] El teléfono terminará la llamada --\n\n"
      end
=end

  end

  def on_success_res_for_register(session)
    print "\n\n[sipper] on_success_res_for_register \n\n"
    print "		<< ACK\n"
    print "		>> INVITE\n"
    print "\n\n[info] -- Llamando... --\n\n"
    session = create_udp_session(SipperConfigurator[:DefaultRIP], SipperConfigurator[:DefaultRP])
    session.request_with('INVITE', 'sip:'+ SipperConfigurator[:ClientUser].to_s() +'@'+ SipperConfigurator[:DefaultRIP])
  end

  def on_bye(session)
    print "\n\n[sipper] on_bye \n\n"
    print "		<< BYE\n"
    print "\n\n[info] -- El teléfono ha colgado. Se finaliza la conversación.\n\n"
    session.respond_with(200)
    session.invalidate(true)
  end

=begin
  def on_media_voice_activity_detected(session)
    print "[sipper] on_media_voice_activity_detected\n"
    #session.set_media_attributes(:rec_spec=>'rec.au')
  end

  def on_media_voice_activity_stopped(session)
    print "[sipper] on_media_voice_activity_stopped\n"
  end

  def on_media_connected(session)
    print "[sipper] on_media_connected\n"
  end

  def on_media_disconnected(session)
    print "[sipper] on_media_disconnected\n"
  end
=end

=begin
  def on_failure_res(session)
    if session.iresponse.code == 401
       r = session.create_request_with_response_to_challenge(session.iresponse.www_authenticate, false,"3050", "3050")
       session.send r
    end
  end
=end

=begin
  def on_ack(session)
    print "on_ack called for "+name+"\n"
  end
=end

end

end
